import { TypedStorageEncoder, TypedStorageDecoder, TypedStorageOptions } from "@nsb/common-tools";

export enum CompactnessType {
  min = 0,
  tight = 0,
  loose = 1,
  max = 1
}
export const compactnessClassName: Record<CompactnessType, string> = {
  [CompactnessType.tight]: 'app_tight',
  [CompactnessType.loose]: 'app_loose',
}

const encoder: TypedStorageEncoder<CompactnessType | null> = data => '' + data;
const decoder: TypedStorageDecoder<CompactnessType | null> = raw => {
  const num = Number(raw)
  if (Number.isNaN(num)) return CompactnessType.min;
  else if (num < CompactnessType.min) return CompactnessType.min;
  else if (num > CompactnessType.max) return CompactnessType.max;
  else return num
}
export const compactnessTypeOptions: TypedStorageOptions<CompactnessType | null> = {
  defaultValue: CompactnessType.loose,
  encoder,
  decoder,
}
